package com.novellotus.iov.carvice.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.PointsBean;
import com.novellotus.iov.carvice.interfaces.AdapterBottomInterface;
import com.novellotus.iov.carvice.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2018/3/26.
 *
 */

public class IntegralListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements AdapterBottomInterface {

    List<PointsBean.RecordsBean> list;
    final int BOTTOM = 100;
    private boolean showBottom = false;

    public IntegralListAdapter(List<PointsBean.RecordsBean> list) {
        if (list == null) {
            this.list = new ArrayList<>();
        } else {
            this.list = list;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == list.size()) {
            return BOTTOM;
        } else {
            return 1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == BOTTOM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_bottom, parent, false);
            return new IntegralListAdapter.BottomHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_integral_list_item, parent, false);
            return new IntegralHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == list.size()) {
            initBottom((IntegralListAdapter.BottomHolder) holder, position);
        } else {
            if (list.get(position) != null)
                initIntegral((IntegralListAdapter.IntegralHolder) holder, position);
        }
    }

    private void initIntegral(IntegralListAdapter.IntegralHolder holder,int position ) {
        holder.tv_integral.setText("+"+list.get(position).getPoint());
        holder.tv_time.setText(TimeUtils.stampTotime(list.get(position).getCreateTime()+""));
        holder.tv_type.setText(list.get(position).getPointTypeDesc());
    }


    private void initBottom(IntegralListAdapter.BottomHolder holder, int position) {
        holder.tv_bottom.setTextColor(Color.parseColor("#333333"));
//        holder.tv_bottom.se
// tAlpha(0.3f);
    }


    @Override
    public int getItemCount() {
        if (showBottom) {
            return list.size() + 1;
        } else {
            return list.size();
        }
    }

    /**
     * @param list 刷新数据
     */
    public void update(List<PointsBean.RecordsBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    /**
     * @param list 添加下拉时的
     */
    public void addData(List<PointsBean.RecordsBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public void showBottomLayout() {
        showBottom = list.size() >= 6;
        notifyDataSetChanged();
    }

    @Override
    public void hideBottomLayout() {
        showBottom = false;
        notifyDataSetChanged();
    }

    class IntegralHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_type)
        TextView tv_type;
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.tv_integral)
        TextView tv_integral;

        IntegralHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    class BottomHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rlt_bottom)
        RelativeLayout rlt_bottom;
        @BindView(R.id.tv_bottom)
        TextView tv_bottom;
        @BindView(R.id.view_left)
        View view_left;
        @BindView(R.id.view_right)
        View view_right;

        BottomHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
